﻿using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Labthink.PCSoft.UniversalHost4.Utility;
using NLog;

namespace Labthink.PCSoft.UniversalHost4.Instrument.Forms
{
    public partial class FrmSystemSet
    {
        private readonly Logger _logger = LogManager.GetCurrentClassLogger();

        public FrmSystemSet()
        {
            InitializeComponent();
            if (!Debugger.IsAttached)
            {
                WindowState = WindowState.Maximized;
            }
            else
            {
                WindowState = WindowState.Normal;

                double height = SystemParameters.FullPrimaryScreenHeight;
                double width = SystemParameters.FullPrimaryScreenWidth;

                Width = 1024;
                Height = 768;

                Top = (height - Height) / 2;
                Left = (width - Width) / 2;
            }
        }

        private void BtnBack_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            TbSettings.SelectedIndex = 0;
            Loading.Visibility = Visibility.Visible;
            LoadPer();
            ReSelectTab();
            Loading.Visibility = Visibility.Collapsed;
            _logger.Debug("FrmSystemSet loaded");
        }

        private void LoadPer()
        {
            //if (Common.Permissions.All(p => p.Code != "Per_ComSetting".Clear()))
            //{
            //    TiCom.Visibility = Visibility.Collapsed;
            //}
            //else
            //{
            //    TiGmp.Visibility = Visibility.Visible;
            //}

            if (Common.Permissions.All(p => p.Code != "Per_SystemBaseInfo".Clear()))
            {
                TiBaseInfo.Visibility = Visibility.Collapsed;
            }
            else
            {
                TiBaseInfo.Visibility = Visibility.Visible;
            }

            if (!Common.BaseInfo.Gmp || Common.Permissions.All(p => p.Code != "Per_GmpSetting".Clear()))
            {
                TiGmp.Visibility = Visibility.Collapsed;
            }
            else
            {
                TiGmp.Visibility = Visibility.Visible;
            }

            if (Common.Permissions.All(p => p.Code != "Per_UserMgmt".Clear()))
            {
                TiUser.Visibility = Visibility.Collapsed;
            }
            else
            {
                TiUser.Visibility = Visibility.Visible;
            }

            if (Common.Permissions.All(p => p.Code != "Per_RoleMgmt".Clear()))
            {
                TiRole.Visibility = Visibility.Collapsed;
            }
            else
            {
                TiRole.Visibility = Visibility.Visible;
            }

            TiOpLog.Visibility = Visibility.Collapsed;
            //if (Common.Permissions.All(p => p.Code != "Per_OperationLog".Clear()))
            //{
            //    TiOpLog.Visibility = Visibility.Collapsed;
            //}
            //else
            //{
            //    TiOpLog.Visibility = Visibility.Visible;
            //}

            if (Common.Permissions.All(p => p.Code != "Per_DataManage".Clear()))
            {
                TiDataManage.Visibility = Visibility.Collapsed;
            }
            else
            {
                TiDataManage.Visibility = Visibility.Visible;
            }

            if (Common.Permissions.All(p => p.Code != "Per_SecuritySetting".Clear()))
            {
                TiSafeInfo.Visibility = Visibility.Collapsed;
            }
            else
            {
                TiSafeInfo.Visibility = Visibility.Visible;
            }

        }

        private void FrmSystemSet_OnClosing(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
            Visibility = Visibility.Hidden;
        }

        public void ReConfigPermission()
        {
            _logger.Debug("ReConfigPermission");
            LoadPer();
            ReSelectTab();
        }

        private void ReSelectTab()
        {
            var x = TbSettings.Items.Cast<TabItem>().FirstOrDefault(ti => ti.Visibility == Visibility.Visible);
            if (x != null)
            {
                x.IsSelected = true;
            }

            UcOperationLog.LoadLogs();
        }

        private void UcSerialPort_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
